/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ve.usb.cep.db.models;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ve.usb.cep.db.SelectObject;
import ve.usb.cep.db.SelectException;
import ve.usb.cep.db.Session;
import ve.usb.cep.db.domainObjects.Pago;
import ve.usb.cep.db.modelExceptions.PagoException;


/**
 *
 * @author Andras
 */
public class PagoModel {
    Connection conn;
    PreparedStatement rowCountStatement;

    public Pago set(Pago curso)
        throws PagoException
    {
        if (0 == curso.getCodigo()){
            add(curso);
        }else{
            update(curso);
        }

        return curso;
    }

    public Pago add(Pago pago)
        throws PagoException
    {

        conn = Session.connection();

        try{
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO pago " +
                "(voucher, monto, inscripcion)" +
                " VALUES (?, ?, ?)");

            pstmt.setString(1, pago.getVoucher());
            System.out.println("EN EL MODELO: nro de deposito -->" + pago.getVoucher());
            pstmt.setInt(2, pago.getMonto());
            pstmt.setInt(3, pago.getInscripcion());
            
            System.out.println("TERMINA POR AQUI!!!!!!!!!!!!1");

            pstmt.executeUpdate();
        }catch(java.sql.SQLException e){
            e.printStackTrace();
            throw new PagoException(e.getMessage());
        }
        return pago;
    }

    public Pago update(Pago pago)
        throws PagoException
    {

        conn = Session.connection();

        try{
            PreparedStatement pstmt = conn.prepareStatement("UPDATE pago " +
              "SET voucher=?, validado_por=?, monto=?, conforme=?, descripcion=?, inscripcion=? " +
              " WHERE codigo=?");

            pstmt.setString(1, pago.getVoucher());
            pstmt.setInt(2, pago.getValidadoPor());
            pstmt.setInt(3, pago.getMonto());
            pstmt.setShort(4, pago.getConforme());
            pstmt.setString(5, pago.getDescripcion());
            pstmt.setInt(6, pago.getInscripcion());
            pstmt.setInt(7, pago.getCodigo());
            pstmt.executeUpdate();
        }catch(java.sql.SQLException e){
            e.printStackTrace();
            throw new PagoException(e.getMessage());
        }
        return pago;
    }

    public void delete(int codigo)
        throws PagoException
    {
        
        conn = Session.connection();
        try{
            PreparedStatement p = conn.prepareStatement("DELETE FROM pago WHERE codigo=?");
            p.setInt(1, codigo);
            p.executeUpdate();
        }catch(SQLException e){
            e.printStackTrace();
            throw new PagoException(e.getMessage());
        }
    }

    public ArrayList<Pago> getCustom(SelectObject s) 
            throws PagoException {

        conn = Session.connection();
        s.setConnection(conn);

        s.setFields(Pago.getFields());
        s.setTypes(Pago.getTypes());
        s.setTable("pago");

        PreparedStatement p;
        try{
            p = s.getSearchQuery();
            rowCountStatement = s.getCountQuery();
        }catch(SelectException e){
            e.printStackTrace();
            throw new PagoException(e.getMessage());
        }

        ArrayList<Pago> arr = new ArrayList<Pago>();
        try{
            ResultSet rs = p.executeQuery();
            while (rs.next()){
                arr.add(new Pago(
                    rs.getInt("codigo"),
                    rs.getString("voucher"),
                    rs.getInt("validado_por"),
                    rs.getInt("monto"),
                    rs.getShort("conforme"),
                    rs.getString("descripcion"),
                    rs.getInt("inscripcion")
                ));
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw new PagoException(e.getMessage());
        }

        return arr;
    }
}
